package com.bw.z.mvp.view.activity;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.content.Intent;
import android.graphics.Color;
import android.view.KeyEvent;
import android.view.View;
import android.view.animation.LinearInterpolator;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.blankj.utilcode.util.LogUtils;
import com.bw.z.R;
import com.bw.z.db.DBManager;
import com.bw.z.db.UserTable;
import com.bw.z.db.UserTableDao;
import com.bw.z.mvp.view.user.activity.InformationActivity;
import com.bw.z.mvp.base.view.BaseActivity;
import com.bw.z.mvp.contract.LoginContract;
import com.bw.z.mvp.di.DaggerLoginComponent;
import com.bw.z.mvp.di.LoginModules;
import com.bw.z.mvp.model.entity.LoginEntity;
import com.bw.z.mvp.presenter.LoginPresenter;
import com.bw.z.utils.SpUtils;
import com.google.gson.Gson;

import java.util.List;

public class LoginActivity extends BaseActivity<LoginPresenter> implements LoginContract.loginView, View.OnClickListener {

    private ImageView loginBack;
    private LinearLayout phoneLoginLl;
    private Button loginBtnNumber;
    private TextView loginSwitchNumber;
    private LinearLayout numberLoginLl;
    private Button loginBtnPhone;
    private TextView loginSwitchPhone;
    private EditText phoneName;
    private EditText phonePwd;
    private TextView loginGoReg;
    private DBManager dbManager;
    private String name, pwd;

    @Override
    public View addStatusView() {
        return f(R.id.login_linear);
    }

    @Override
    public int bindLayout() {
        return R.layout.activity_login;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.login_back:
                // 退出该页面
                finish();
                break;
            case R.id.login_switch_number:
                LogUtils.e("账号登录 -> 手机登录");
                AnimatorSet animatorSetNumber = new AnimatorSet();
                animatorSetNumber.setDuration(600);
                animatorSetNumber.setInterpolator(new LinearInterpolator());

                ObjectAnimator alphaNumber = ObjectAnimator.ofFloat(numberLoginLl, "alpha", 1, 0);
                ObjectAnimator rotationNumber = ObjectAnimator.ofFloat(numberLoginLl, "translationY", 0, 30);
                //顺序播放
                animatorSetNumber.playTogether(alphaNumber, rotationNumber);
                animatorSetNumber.start();

                animatorSetNumber.addListener(new AnimatorListenerAdapter() {
                    @Override
                    public void onAnimationEnd(Animator animation) {
                        super.onAnimationEnd(animation);
                        numberLoginLl.setVisibility(View.GONE);
                        phoneLoginLl.setVisibility(View.VISIBLE);
                        AnimatorSet animatorSetNumber2 = new AnimatorSet();
                        animatorSetNumber2.setDuration(600);
                        animatorSetNumber2.setInterpolator(new LinearInterpolator());

                        ObjectAnimator alphaNum2 = ObjectAnimator.ofFloat(phoneLoginLl, "alpha", 0, 1);
                        ObjectAnimator rotationNum2 = ObjectAnimator.ofFloat(phoneLoginLl, "translationY", 20, 0);
                        //顺序播放
                        animatorSetNumber2.playTogether(alphaNum2, rotationNum2);
                        animatorSetNumber2.start();
                        animatorSetNumber2.addListener(new AnimatorListenerAdapter() {
                            @Override
                            public void onAnimationEnd(Animator animation) {
                                super.onAnimationEnd(animation);
                                numberLoginLl.setVisibility(View.GONE);
                            }
                        });
                    }
                });
                break;
            case R.id.login_switch_phone:
                // 手机登录 -> 账号登录
                LogUtils.e("手机登录 -> 账号登录");
                AnimatorSet animatorSetPhone = new AnimatorSet();
                animatorSetPhone.setDuration(600);
                animatorSetPhone.setInterpolator(new LinearInterpolator());

                ObjectAnimator alphaPhone = ObjectAnimator.ofFloat(phoneLoginLl, "alpha", 1, 0);
                ObjectAnimator rotationPhone = ObjectAnimator.ofFloat(phoneLoginLl, "translationY", 0, 30);
                //顺序播放
                animatorSetPhone.playTogether(alphaPhone, rotationPhone);
                animatorSetPhone.start();

                animatorSetPhone.addListener(new AnimatorListenerAdapter() {
                    @Override
                    public void onAnimationEnd(Animator animation) {
                        super.onAnimationEnd(animation);
                        numberLoginLl.setVisibility(View.VISIBLE);
                        phoneLoginLl.setVisibility(View.GONE);
                        AnimatorSet animatorSetPhone2 = new AnimatorSet();
                        animatorSetPhone2.setDuration(600);
                        animatorSetPhone2.setInterpolator(new LinearInterpolator());

                        ObjectAnimator alphaPhone2 = ObjectAnimator.ofFloat(numberLoginLl, "alpha", 0, 1);
                        ObjectAnimator rotationPhone2 = ObjectAnimator.ofFloat(numberLoginLl, "translationY", 30, 0);
                        //顺序播放
                        animatorSetPhone2.playTogether(alphaPhone2, rotationPhone2);
                        animatorSetPhone2.start();
                        animatorSetPhone2.addListener(new AnimatorListenerAdapter() {
                            @Override
                            public void onAnimationEnd(Animator animation) {
                                super.onAnimationEnd(animation);
                                phoneLoginLl.setVisibility(View.GONE);
                            }
                        });
                    }
                });
                break;
            case R.id.login_goReg:
                // 跳转注册页面
                Intent intent = new Intent(LoginActivity.this, RegisterActivity.class);
                startActivity(intent);
                break;
            case R.id.login_btn_phone:
                name = phoneName.getText().toString().trim();
                pwd = phonePwd.getText().toString().trim();
                if (name.equals("") || pwd.equals("")) {
                    Toast.makeText(LoginActivity.this, "不能为空", Toast.LENGTH_SHORT).show();
                } else {
                    int i = Integer.parseInt(name);
                    LogUtils.e("i:" + i);
                    p.getLoginData(i, pwd);
                }
                break;
        }
    }

    @Override
    public void initView() {
        getColors(Color.WHITE);
        loginBack = findViewById(R.id.login_back);
        phoneLoginLl = findViewById(R.id.phone_login_ll);
        loginBtnNumber = findViewById(R.id.login_btn_number);
        loginSwitchNumber = findViewById(R.id.login_switch_number);
        numberLoginLl = findViewById(R.id.number_login_ll);
        loginBtnPhone = findViewById(R.id.login_btn_phone);
        loginSwitchPhone = findViewById(R.id.login_switch_phone);
        phoneName = findViewById(R.id.phone_name);
        phonePwd = findViewById(R.id.phone_pwd);
        loginGoReg = findViewById(R.id.login_goReg);

        loginBack.setOnClickListener(this::onClick);
        loginSwitchNumber.setOnClickListener(this::onClick);
        loginSwitchPhone.setOnClickListener(this::onClick);
        loginGoReg.setOnClickListener(this::onClick);
        loginBtnPhone.setOnClickListener(this::onClick);

    }

    @Override
    public void inject() {
        DaggerLoginComponent.builder().loginModules(new LoginModules(this)).build().injectLogin(this);
    }

    @Override
    public void initData() {
    }

    @Override
    public void refreshLogin(LoginEntity loginEntity) {
        String values = loginEntity.getValues();
        UserTable user = new Gson().fromJson(values, UserTable.class);
        if (loginEntity.status.equals("200")) {
            Intent intentBroadcast = new Intent();
            intentBroadcast.setAction("login_nickname");
            intentBroadcast.putExtra("username", user.getUser_name());
            sendBroadcast(intentBroadcast);
            dbManager = new DBManager(LoginActivity.this);
            UserTableDao userTableDao = dbManager.getDaoSession().getUserTableDao();
            List<UserTable> userTables = userTableDao.loadAll();
            boolean isCheck = false;

            for (int i = 0; i < userTables.size(); i++) {
                if (userTables.get(i).getUser_phone() == user.getUser_phone()) {
                    isCheck = true;
                    break;
                }
            }

            if (!isCheck) {
                user.setUser_integral(1);
                userTableDao.insert(user);
            }
            LogUtils.json(userTableDao.loadAll());
            Intent intent = new Intent(LoginActivity.this, InformationActivity.class);
            SpUtils.saveDataLong("user_id", userTableDao.loadAll().get(0).getId());
            startActivity(intent);
            finish();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        dbManager = null;
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            finish();
        }
        return super.onKeyDown(keyCode, event);
    }
}